package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.Floor;
import com.zzyl.nursing.mapper.FloorMapper;
import com.zzyl.nursing.service.IFloorService;
import com.zzyl.nursing.vo.TreeVO1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 楼层Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
@Slf4j
public class FloorServiceImpl extends ServiceImpl<FloorMapper, Floor> implements IFloorService {
    @Autowired
    private FloorMapper floorMapper;

    /**
     * 查询楼层
     *
     * @param id 楼层主键
     * @return 楼层
     */
    @Override
    public Floor selectFloorById(Long id) {
        return getById(id);
    }

    /**
     * 新增楼层
     *
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int insertFloor(Floor floor) {
        return save(floor) ? 1 : 0;
    }

    /**
     * 修改楼层
     *
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int updateFloor(Floor floor) {
        return updateById(floor) ? 1 : 0;
    }

    /**
     * 批量删除楼层
     *
     * @param ids 需要删除的楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除楼层信息
     *
     * @param id 楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有楼层（负责老人）
     */
    @Override
    public List<Floor> selectAllByNur() {
        return floorMapper.selectAllByNur();
    }

//    @Override
//    public List<TreeVO> getRoomAndBedByBedStatus(Long status) {
//        return floorMapper.getRoomAndBedByBedStatus(status);
//    }

    @Override
    public List<TreeVO1> getRoomAndBedByBedStatus(Long status) {
        // 1. 查询原始数据
        List<TreeVO1> flatData = floorMapper.getRoomAndBedByBedStatus(status);

        // 2. 构建树形结构
        return buildTreeStructure(flatData);
    }

    /**
     * 将扁平数据构建成树形结构
     * @param flatData 扁平数据
     * @return 树形结构数据
     */
    private List<TreeVO1> buildTreeStructure(List<TreeVO1> flatData) {
        // 使用LinkedHashMap保持插入顺序
        Map<String, TreeVO1> floorMap = new LinkedHashMap<>();
        Map<String, TreeVO1> roomMap = new HashMap<>();

        // 遍历所有数据项
        for (TreeVO1 item : flatData) {
            // 创建或获取楼层节点
            if (!floorMap.containsKey(item.getFloorId())) {
                TreeVO1 floorNode = new TreeVO1();
                floorNode.setValue(item.getFloorId());
                floorNode.setLabel(item.getFloorName());
                floorNode.setChildren(new ArrayList<>());
                floorMap.put(item.getFloorId(), floorNode);
            }

            // 创建或获取房间节点
            String roomKey = item.getFloorId() + "_" + item.getRoomId();
            if (!roomMap.containsKey(roomKey)) {
                TreeVO1 roomNode = new TreeVO1();
                roomNode.setValue(item.getRoomId());
                roomNode.setLabel(item.getRoomCode());
                roomNode.setChildren(new ArrayList<>());
                roomMap.put(roomKey, roomNode);

                // 将房间添加到对应楼层的子节点中
                floorMap.get(item.getFloorId()).getChildren().add(roomNode);
            }

            // 创建床位节点
            TreeVO1 bedNode = new TreeVO1();
            bedNode.setValue(item.getValue());
            bedNode.setLabel(item.getLabel());

            // 将床位添加到对应房间的子节点中
            roomMap.get(roomKey).getChildren().add(bedNode);
        }

        // 返回楼层节点列表
        return new ArrayList<>(floorMap.values());
    }

}